/***************************************************************************
 *   Copyright (C) 2007 by saa   *
 *   root@saa   *
 ***************************************************************************/

#ifndef TELNETCLIENT_H
#define TELNETCLIENT_H

#include <QtDebug>
#include <QStringList>

#include "qttelnet.h"
#include "routeserver.h"
#include "compilemode.h"

class TelnetClient : public QtTelnet
{
    Q_OBJECT
            
    public:
        TelnetClient(QObject * parent = 0);
        void setTelnetParams(const RouteServer & routeServer) {m_routeServer = routeServer;}
        void setTargetList(const QStringList & targetList) { m_targetList = targetList; }
        void run( );
        
    public slots:
        void nodeListTolinkList(QList<tracerouteNode>);
        void log(const QString & message, const LogFlag::LogLevels = LogFlag::TelnetClient) const;

    signals:
        void ready(QObject * sender);
        void saveResults(const LinkList & linkList);
        void parseString(const QString& result);
        void tracerouteToTargetStarted(const QString & routeServer, const QString & target);
        void tracerouteToTargetFinished(const QString & routeServer, const QString & target);
        void logTelnetMessage(const QString & message, const LogFlag::LogLevels = LogFlag::TelnetClient) const;
                
    private slots:
        void telnetMessage(const QString &msg);
        void telnetLoginRequired();
        void telnetLoginFailed();
        void telnetLoggedOut();
        void telnetLoggedIn();
        void telnetConnectionError(QAbstractSocket::SocketError error);
        void suspend();
        void tcpConnected();

    private:

        void nextTarget();
        QString stripCR(const QString &msg);
        
        RouteServer m_routeServer;
        QStringList m_targetList;
        QString m_currentTarget;
        
        QString tracerouteResult;
        bool tracerouteState;
        int emptyStringCount;
        int stringAsteriskCount;
        QString serverIP;
        unsigned int m_attemptLimit;
};

#endif
